package com.gzbugu.bugusys.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.RequestAware;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.gzbugu.bugusys.util.HibernateUtils;
import com.gzbugu.bugusys.vo.Attendance;
import com.gzbugu.bugusys.vo.Staff;
import com.gzbugu.bugusys.vo.Type;
import com.gzbugu.bugusys.dao.IBaseDAO;

@Controller
public class AttendanceRegisterAction extends ActionSupport implements
		RequestAware {

	@Resource(name = "baseDAO")
	private IBaseDAO baseDAO;

	private Map<String, Object> request;
	private Attendance attendance;
	private Integer attendanceTypeId;
	private String staffId;
	List errorList = null;
	Staff staff = null;
	Type attendanceType = null;

	public String execute() {
		String result = "";
		errorList = new ArrayList();
		Session session = null;
		Transaction tx = null;
		System.out.println(this.staffId);
		if (this.invalidate()) {
			try {
				session = HibernateUtils.getSession();
				tx = session.beginTransaction();
				attendanceType = (Type) baseDAO.findById(this.attendanceTypeId.shortValue(),
						com.gzbugu.bugusys.fields.VoClasseFields.TYPE);
				attendance.setType(attendanceType);
				attendance.setStaff(staff);
				System.out.println(staff.getName());
				System.out.println(attendanceType.getName());
				session.save(attendance);
				result = "success";
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				tx.rollback();
			} finally {
				HibernateUtils.closeSession(session);
			}
		} else {
			request.put("errorList", errorList);
			result = "success";
		}
		return result;
	}

	public boolean invalidate() {
		boolean flag = true;
		if ("".equals(this.staffId) || this.staffId == null) {
			this.errorList.add("员工编号不能为空！");
		} else {
			staff = (Staff) baseDAO.findById(this.staffId,
					com.gzbugu.bugusys.fields.VoClasseFields.STAFF);
			if (staff == null) {
				this.errorList.add("该员工不存在！");
				flag = false;
			}
		}
		if (attendance.getEnd().getTime() - attendance.getStart().getTime() == 0) {
			this.errorList.add("起始时间有冲突！");
			flag = false;
		}
		if (this.attendanceTypeId == 0) {
			this.errorList.add("未选择考勤事件类型！");
		}
		return flag;
	}

	public String getStaffId() {
		return staffId;
	}

	public void setStaffId(String staffId) {
		this.staffId = staffId;
	}

	public Attendance getAttendance() {
		return attendance;
	}

	public void setAttendance(Attendance attendance) {
		this.attendance = attendance;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public Integer getAttendanceTypeId() {
		return attendanceTypeId;
	}

	public void setAttendanceTypeId(Integer attendanceTypeId) {
		this.attendanceTypeId = attendanceTypeId;
	}

}
